package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;

/**
 * <p>Title: ServiceZMCE43.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author  高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-19 下午02:40:20  高强  Create
 *</pre>
 */
public class ServiceZMCE43 extends ServiceEPBase {
	
	
	/**
	 * 设备变动新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		boolean validateValue = validate(inInfo, "ZMCEEquipmentOrder.validateForMaintenanceApply", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "keyID", inInfo.getBlock("result").getCellStr(0, "MaintenanceApplykeyID")); 
			EiInfo outInfo =super.update(inInfo, "ZMCEMaintenanceApply.modifyList");//修改状态为计划
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_EQUIPMENTORDER"));//主键
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdDepartmentId", inInfo.getBlock("result").getCellStr(0, "fdDepartmentId"));
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdDepartment", Department.getInstance().getDepartmentNameById(inInfo.getBlock("result").getCellStr(0, "fdDepartmentId")));
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdExecutionDepartment", Department.getInstance().getDepartmentNameById(inInfo.getBlock("result").getCellStr(0, "fdExecutionDepartmentId")));
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdExecutionPerson", User.getInstance().getNameById(inInfo.getBlock("result").getCellStr(0, "fdExecutionPersonId")));
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdFounder", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdFounderId", UserSession.getLoginName());
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdFounderDepId", UserSession.getInSessionProperty( "orgCode" ));
			if("执行".equals(inInfo.getBlock("result").getCell(0, "fdStatus")))
			{
				inInfo.getBlock("result").setCell(0, "fdTrueBeganTime", StaticTools.getDateTime());
			
				if("大修".equals(inInfo.getBlock("result").getCell(0, "fdType"))||"维修".equals(inInfo.getBlock("result").getCell(0, "fdType")))
				{
					if(Equipment.updateEquipmentStatus("维修", "不完好", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"))<=0)
					{
						inInfo.setStatus(-1);
						return inInfo;
					}
				}
			}
			outInfo = super.insert(inInfo, "ZMCEEquipmentOrder.insert");
			return outInfo;
		}
	}
	
	/**
	 * 编号重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		params.put("fdCode", inInfo.getBlock("result").getCellStr(0, "fdCode"));
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("编号有重复,不能提交");
			return true;
		}
		return false;
	}
	
	

}
